/*
PearReview - The peer review assistant.
Copyright (C) 2009  Dimitrij Pankratz, Anton Musichin
http://www.pearreview.com, info@pearreview.com

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/

package com.pearreview.ui.datepicker;

import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.ImageIcon;
import javax.swing.JLabel;

/**
 * This class is for displaying buttons in the date picker.
 *
 */
public class DatePickerButton extends JLabel {

	private static final long serialVersionUID = 1450009458855929002L;
	
	private boolean mouseOver = false;
	private boolean holiday = false;
	private boolean today = false;
	private boolean placeHolder;
	private ActionListener clickAction;
	
	private int day = 0;
	
	private ImageIcon normalIcon;
	private ImageIcon overIcon;
	private ImageIcon holidayIcon;
	
	private MouseAdapter mouseListener;
	
	public DatePickerButton() {
		setPreferredSize(new Dimension(21, 21));
		setMinimumSize(new Dimension(21, 21));
		setMaximumSize(new Dimension(21, 21));
		
		setPlaceHolder(false);
		
		createMouseLister();
		addMouseListener(mouseListener);
		
		normalIcon = getResourceIcon("calendar_btn.png");
		overIcon = getResourceIcon("calendar_over_btn.png");
		holidayIcon = getResourceIcon("calendar_holiday_btn.png");
	}
	
	/**
	 * Returns true if the button is marked as today button,
	 * otherwise false.
	 * 
	 * @return
	 */
	public boolean isToday() {
		return today;
	}

	/**
	 * Allows to mark a button as holiday button. Will display the button
	 * in different way to not today button.
	 * 
	 * @param today
	 */
	public void setToday(boolean today) {
		this.today = today;
	}
	
	/**
	 * Returns true if the button is marked as holiday button,
	 * otherwise false.
	 * 
	 * @return
	 */
	public boolean isHoliday() {
		return holiday;
	}

	/**
	 * Allows to mark a button as holiday button. Will display the button
	 * in different way to not holiday button.
	 * 
	 * @param holiday
	 */
	public void setHoliday(boolean holiday) {
		this.holiday = holiday;
	}
	
	public boolean isPlaceHolder() {
		return placeHolder;
	}
	
	public void setPlaceHolder(boolean b) {
		placeHolder = b;
		if(!isPlaceHolder()) {
			setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
		} else {
			setCursor(Cursor.getDefaultCursor());
		}
		repaint();
	}

	/**
	 * Returns the action listener of button for mouse released event.
	 * 
	 * @return
	 */
	public ActionListener getClickAction() {
		return clickAction;
	}


	/**
	 * Sets the action listener to the button, which will be performed,
	 * if mouse is released over this button.
	 * 
	 * @param clickAction
	 */
	public void setClickAction(ActionListener clickAction) {
		this.clickAction = clickAction;
	}
	
	public void setDay(int day) {
		this.day = day;
		repaint();
	}
	
	public int getDay() {
		return day;
	}
	
	public void setCalendarDay(CalendarDay day) {
		if(day != null) {
				setHoliday(day.isHoliday());
				setToolTipText("<html>" +
								"<b>" + day.getName() + "</b>" +
								"<br/><br/>" +
								day.getComment() +
								"</html>");
		} else {
			setHoliday(false);
			setToolTipText(null);
		}
	}

	/**
	 * Returns a resource icon
	 * @param name name of icon
	 * @return
	 */
	private ImageIcon getResourceIcon(String name) {
		return new ImageIcon(getClass().getResource("images/" + name));
	}
	
	@Override
	public void paintComponent(Graphics g) {
		Graphics2D g2d = (Graphics2D) g;
		
		if(isPlaceHolder()) {
			return;
		}
		
		if(today || mouseOver) {
			g2d.drawImage(overIcon.getImage(), 0, 0, this);
		} else if(holiday) {
			g2d.drawImage(holidayIcon.getImage(), 0, 0, this);
		} else {
			g2d.drawImage(normalIcon.getImage(), 0, 0, this);
		}
		
		FontMetrics fm = g.getFontMetrics();
		
		g2d.drawString(getText(), 
				(getWidth()-fm.stringWidth(getText()))/2, 
				(getHeight()-fm.getHeight())/2+fm.getAscent());
	}
	
	@Override
	public String getText() {
		return getDay() > 0 ? Integer.toString(getDay()) : "";
	}
	
	private void createMouseLister() {
		mouseListener = new MouseAdapter() {
			public void mouseEntered(MouseEvent mouseEvent) {
				mouseOver = true;
				repaint();
			}
			  
			public void mouseExited(MouseEvent mouseEvent) {
				mouseOver = false;
				repaint();
			}
			  
			public void mousePressed(MouseEvent mouseEvent) {
			}
			  
			public void mouseReleased(MouseEvent mouseEvent) {
				if(clickAction == null) {
					return;
				}
				
				clickAction.actionPerformed(new ActionEvent(
						DatePickerButton.this, 1, "mouseReleased"));
			}
		};
	}
}
